package com.ruoyi.common.tcp;
import java.util.*;
import java.util.concurrent.TimeUnit;


import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import io.netty.channel.ChannelFuture;
@Component
public class NettyTaskJob {




    /**
     * 通过netty客户端定时向所有长连接的设备请求数据
     * 每1S获取一次,首次延迟10s执行
     */
    @Scheduled(initialDelay = 10, fixedRate = 1, timeUnit = TimeUnit.SECONDS)
    private void instantaneousFlow() {
        try {
            Map<String, ChannelFuture> channels = NettyClient.channels;
            Set<String> keySet = channels.keySet();
            for (String key : keySet) {
                ChannelFuture channel = channels.get(key);
                System.out.println("key = " + key + " ,channel =  " +channel);
                // 读取4-20mA 电流值
                if(key.contains("192.168.1.7:8234")){
                    NettyClient.sendMsg(channel,"000100000006010400000010");
                }else if(key.contains("192.168.1.9:8234")){
                    //NettyClient.sendMsg(channel,"000100000006010400000002");
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
